﻿using Furion.DependencyInjection;
using Furion.LinqBuilder;
using OnWork.DataCenter.BLL.Base;
using OnWork.DataCenter.BLL.Base.Dto;
using OnWork.DataCenter.DAL.DataCenter;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OnWork.DataCenter.BLL.DataCenter
{
    public class DSDataBaseBLL : BaseBLL<DSDataBase>, IScoped
    {
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<SQLReturn> AddDataAsync(DSDataBase input)
        {
            input.Name = input.Name.Trim();

            bool isExit = await base.Queryable().Where(x => x.Name == input.Name).AnyAsync();
            if (isExit)
            {
                return SQLReturn.AddError($"错误:数据连接名称:{input.Name}已存在!");
            }

            var rows = await base.AddAsync(input); //添加
            return SQLReturn.AddSuccess();
        }

        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public async Task<SQLReturn> DeleteDataAsync(long[] ids)
        {
            if (ids == null || ids.Length == 0)
            {
                return SQLReturn.DelError($"错误:删除Id不存在!");
            }
            var rows = await base.DeleteAsync<DSDataBase>(ids);
            return SQLReturn.DelSuccess(rows);
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<SQLReturn> UpdateDataAsync(DSDataBase input)
        {
            var old = await base.SingleByIdAsync(input.ID);
            input.Name = input.Name.Trim();
            if (!string.IsNullOrEmpty(input.Name))
            {
                if (Queryable().Where(x => x.Name == input.Name && x.Name != old.Name).Any())
                {
                    return SQLReturn.UpdateError($"错误:数据连接名称:{input.Name}已存在!");
                }
            }
            var rows = await base.UpdateAsync(input);
            return SQLReturn.UpdateSuccess(rows);
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="option"></param>
        /// <returns></returns>
        public async Task<SQLReturn<List<DSDataBase>>> QueryPagedListAsync(BaseQueryPageInput option)
        {
            var querysql = option.ToQuerySQL<DSDataBase>();

            RefAsync<int> total = 0;
            var query = await Queryable<DSDataBase>()
                .WhereIF(!string.IsNullOrEmpty(querysql.Where), querysql.Where)
                .OrderByIF(!string.IsNullOrEmpty(querysql.Order), $"{querysql.Order}")
                .ToPageListAsync(option.PageIndex, option.PageItems, total);
            return SQLReturn<DSDataBase>.QueryListSuccess(query, total.Value);
        }


        /// <summary>
        /// 获取全部可用的数据连接
        /// </summary>
        /// <returns></returns>
        public async Task<List<DSDataBase>> GetALLAsync()
        {
            return await Queryable().ToListAsync();
        }

        /// <summary>
        /// 获取全部可用的数据连接
        /// </summary>
        /// <returns></returns>
        public List<DSDataBase> GetALL()
        {
            return Queryable().ToList();
        }
    }
}
